package aryn;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JComponent;

public class LayeredAnimation {
	
	private ArrayList<BufferedImage[]> layers;
	private int currentLayerIndex;
	private int currentFrameIndex;
	private long time;
	private int delay;
	
	
	/**	You should use always this constructor for big layered animations.
	 * 	
	 * 	As pretty obvious, you pass as arguments the layers of the animation stored in an ArrayList
	 * 	and the delay between each frame.
	 */
	public LayeredAnimation(ArrayList<BufferedImage[]> layers, int delay ) {
		this.layers = layers;
		this.currentLayerIndex = 0;
		this.currentFrameIndex = 0;
		this.time = System.currentTimeMillis();
		this.delay = delay;
	}
	
	
	/**	This is a fast version for two layered animations (or the version for who hates ArrayList) :D
	 * 
	 * 	You set the first two layered animations and their delay. Next u can add more frames using
	 * 	addLayer method and remove frames using removeLayer method.
	 */
	public LayeredAnimation(BufferedImage[] firstFrames, BufferedImage[] secondFrames, int delay ) {
		layers = new ArrayList<BufferedImage[]>();
		layers.add(firstFrames);
		layers.add(secondFrames);
		this.currentLayerIndex = 0;
		this.currentFrameIndex = 0;
		this.time = System.currentTimeMillis();
		this.delay = delay;
	}
	
	/**	Plays the animation stored in the currentLayerIndex. As default his value is 0 (first layer).
	 * @param component is the component where you're going to draw the animation. Usually "this" inside
	 * some class extending JPanel.
	 * @param x, y are the coordinates where you want to draw the animation.
	 */
	public void play(Graphics g, JComponent component, int x, int y) {
		if (layers.get(currentLayerIndex) != null) {
			BufferedImage[] frames = layers.get(currentLayerIndex);
			g.drawImage( frames[currentFrameIndex], x, y, component);	
			
			if (System.currentTimeMillis() > time + delay) {
				currentFrameIndex++;
				if (currentFrameIndex == frames.length)
					currentFrameIndex = 0;
				time = System.currentTimeMillis();
			}
		}
	}

	
	
	  // ******************* \\
	 //	  GETTERS & SETTERS	  \\
	// *********************** \\

	public ArrayList<BufferedImage[]> getLayers() {
		return layers;
	}
	public void setLayers(ArrayList<BufferedImage[]> layers) {
		this.layers = layers;
	}
	public BufferedImage[] getLayer(int index) {
		return layers.get(index);
	}
	public void setLayer(BufferedImage[] layer, int index) {
		this.layers.set(index, layer);
	}
	public void addLayer(BufferedImage[] layer) {
		this.layers.add(layer);
	}
	public void addLayer(BufferedImage[] layer, int index) {
		this.layers.add(index, layer);
	}
	public void removeLayer(BufferedImage[] layer) {
		this.layers.remove(layer);
	}
	public void removeLayer(int index) {
		this.layers.remove(index);
	}
	public int getCurrentLayerIndex() {
		return currentLayerIndex;
	}
	public void setCurrentLayerIndex(int currentLayer) {
		this.currentLayerIndex = currentLayer;
	}
	public int getCurrentFrameIndex() {
		return currentFrameIndex;
	}
	public void setCurrentFrameIndex(int currentFrame) {
		this.currentFrameIndex = currentFrame;
	}
	public int getDelay() {
		return delay;
	}
	public void setDelay(int delay) {
		this.delay = delay;
	}
	
}
